sources <- c("ESPN", "FantasyData", "FantasyPros", "FantasySharks", 
             "Yahoo", "FantasyFootballNerd", 
             "RTSports", "Walterfootball")

scrape <- scrape_data(src = sources,
                      pos = c('QB', 'RB', 'WR', 'TE', 'DST'),
                      season = 2020, 
                      week = week)

Simulation Time!

n_sims <- 10000

tic()
sim_lu <- map_df(1:n_sims, generate_lineup) %>%
  rename(pts_base = points) %>%  
  mutate(position = factor(position, 
                           levels = c("QB", "RB", "WR", "TE", "DST"))) %>% 
  select(lineup, Name, team, position, pts_base, pts_pred, sd_pts, Salary)
toc()
## 1134.744 sec elapsed

Results

sim_lu %>%
  filter(lineup<=3) %>%
  arrange(lineup, position, desc(pts_pred)) %>%
  knitr::kable() %>%
  kable_styling() %>%
  column_spec(1, bold=TRUE) %>%
  collapse_rows(columns = 1, valign = 'top')
lineup Name team position pts_base pts_pred sd_pts Salary
1 Cam Newton NEP QB 20.27342 24.31575 6.2847782 8000
Kareem Hunt CLE RB 15.69250 19.50780 1.9829775 7000
Derrick Henry TEN RB 19.24826 18.74099 0.6645857 9000
David Montgomery CHI RB 14.79494 14.85538 0.9117097 5900
Adam Thielen MIN WR 15.46500 15.96447 1.6456860 7400
Darius Slayton NYG WR 11.48500 14.47273 1.6827510 6000
Marquise Brown BAL WR 12.29500 13.20064 1.2602100 6300
Eric Ebron PIT TE 7.08500 10.55215 2.5723110 5200
Indianapolis Colts IND DST 9.45000 11.41963 2.2980300 5000
2 Kirk Cousins MIN QB 18.17729 20.38301 0.8728278 7100
Frank Gore NYJ RB 11.08930 20.65476 5.0239310 5000
Derrick Henry TEN RB 19.24826 19.54337 0.6645857 9000
David Montgomery CHI RB 14.79494 15.25897 0.9117097 5900
Julio Jones ATL WR 11.20500 19.59526 3.5804790 8200
Adam Thielen MIN WR 15.46500 19.24003 1.6456860 7400
Terry McLaurin WAS WR 13.04000 14.18348 1.0378200 6900
Jimmy Graham CHI TE 10.98000 12.27635 1.8977280 5400
New England Patriots NEP DST 8.42000 12.36831 2.4018120 4800
3 Tom Brady TBB QB 19.82297 26.47260 4.7888852 7500
Derrick Henry TEN RB 19.24826 18.76410 0.6645857 9000
Alexander Mattison MIN RB 14.89183 17.41067 2.7252777 7000
Frank Gore NYJ RB 11.08930 14.26942 5.0239310 5000
Calvin Ridley ATL WR 15.82500 15.81566 0.1111950 8600
Jamison Crowder NYJ WR 13.78000 14.61793 1.1193630 6600
Robby Anderson CAR WR 12.40000 14.32149 1.9273800 6500
Cameron Brate TBB TE 5.74500 10.34296 2.3054430 4800
Indianapolis Colts IND DST 9.45000 12.73332 2.2980300 5000
ggplotly(sim_lu %>% 
           group_by(Name, position, Salary) %>% 
           dplyr::summarize(lu = n_distinct(lineup)) %>% 
           ungroup() %>% 
           group_by(position) %>% 
           top_n(10, lu) %>% 
           ungroup() %>% 
           arrange(position, desc(lu)) %>% 
           mutate(Name = factor(Name),
                  Name = fct_reorder(Name, lu),
                  pct = round(lu / n_sims, 3) * 100) %>% 
           ggplot(aes(x = Name, y = pct, fill = Salary,
                      text = paste(Name, "in", lu, "lineups with", Salary, "salary"))) +
           geom_bar(stat = "identity") +
           facet_wrap(~position, ncol = 2, scales = "free_y") +
           coord_flip() +
           scale_fill_viridis_c() +
           xlab("") +
           ylab("Lineups (thousands)") +
           ggtitle("Top 10 Players By Position")) %>% 
  ggplotly(tooltip = "text")
plyr_lu <- sim_lu %>%
  group_by(Name, position) %>%
  dplyr::summarize(lu=n_distinct(lineup)) %>%
  ungroup() 

ggplotly(projections %>% 
  filter(avg_type=='weighted') %>%
  mutate(Name = ifelse(pos=="DST", last_name, paste(first_name, last_name))) %>%
  inner_join(fan_duel, by = c("Name", "position")) %>%
  select(Name, team, position, points, Salary, sd_pts) %>%
  left_join(plyr_lu, by='Name') %>%
  replace_na(list(lu=0)) %>%
  mutate(lu_bin=ifelse(lu==0, '0 Lineups', '>=1 Lineups'),
         lu_5=cut(lu,5, labels = FALSE)) %>%
  ggplot(aes(x=Salary, y=points, color=lu_bin, size=sd_pts, text=Name)) +
  geom_point() +
  theme_minimal() +
  scale_color_manual(values = c('red', 'blue'), name="") +
  geom_smooth(inherit.aes = FALSE, aes(x=Salary, y=points), method = 'lm') +
  ylab('Projected Points') +
  xlab('Salary') +
  ggtitle('Who makes it into Optimized Lineups?') +
  scale_x_continuous(labels=scales::dollar))
sim_lu %>%
  group_by(lineup) %>%
  mutate(lineup_pts=sum(pts_pred)) %>%
  group_by(lineup, position) %>%
  mutate(n = n()) %>%
  select(lineup, position, n, lineup_pts) %>%
  distinct() %>%
  spread(key=position, value=n) %>%
  filter(RB >= 2, TE >= 1, WR >= 3) %>%
  mutate(flex = case_when(RB==3 ~ 'RB',
                          TE==2 ~ 'TE',
                          WR==4 ~ 'WR')) %>%
  group_by(flex) %>%
  dplyr::summarize(median_pts = round(median(lineup_pts), 3),
                   cases = n()) %>%
  knitr::kable() %>%
  kable_styling(full_width = FALSE)
flex median_pts cases
RB 143.524 9847
TE 145.609 92
WR 145.161 61
lu_df <- sim_lu %>%
  group_by(lineup) %>%
  dplyr::summarize(lineup_pts=sum(pts_pred),
                   lineup_sd=sum(sd_pts)) %>%
  ungroup()

pto <- psel(lu_df, low(lineup_sd) * high(lineup_pts))


ggplot(lu_df, aes(y=lineup_pts, x=lineup_sd)) +
  geom_point() +
  geom_point(data=pto, size=5) +
  ylab('Lineup Points') +
  xlab('Lineup Points St Dev') +
  ggtitle('Lineup Points vs Uncertainty',
          subtitle = 'Pareto Lineups Bolded')

psel(lu_df, low(lineup_sd) * high(lineup_pts)) %>%
  left_join(sim_lu, by='lineup') %>%
  group_by(lineup) %>%
  arrange(lineup_pts, position, desc(Salary)) %>%
  select(lineup, lineup_pts, lineup_sd, Name, team, position, pts_pred, sd_pts, Salary) %>%
  mutate_at(vars(lineup_pts, lineup_sd, pts_pred, sd_pts), function(x) round(x, 2)) %>%
  knitr::kable() %>%
  kable_styling(fixed_thead = T) %>%
  column_spec(1:3, bold=TRUE) %>%
  collapse_rows(columns = 1:3, valign = 'top') %>%
  scroll_box(height = '700px', width = '100%')
lineup lineup_pts lineup_sd Name team position pts_pred sd_pts Salary
5760 132.25 7.88 Ben Roethlisberger PIT QB 21.73 1.00 7600
Derrick Henry TEN RB 19.26 0.66 9000
James Conner PIT RB 15.54 0.22 7100
David Montgomery CHI RB 14.96 0.91 5900
Calvin Ridley ATL WR 15.87 0.11 8600
Jamison Crowder NYJ WR 13.62 1.12 6600
DeVante Parker MIA WR 12.24 0.25 6400
Jimmy Graham CHI TE 10.56 1.90 5400
Detroit Lions DET DST 8.47 1.70 3400
5552 134.02 8.39 Aaron Rodgers GBP QB 23.58 1.17 8400
Derrick Henry TEN RB 19.24 0.66 9000
James Conner PIT RB 15.30 0.22 7100
David Montgomery CHI RB 14.57 0.91 5900
Terry McLaurin WAS WR 13.91 1.04 6900
Jamison Crowder NYJ WR 14.27 1.12 6600
DeVante Parker MIA WR 13.05 0.25 6400
Jimmy Graham CHI TE 10.50 1.90 5400
Tennessee Titans TEN DST 9.60 1.11 4000
6190 135.73 8.48 Lamar Jackson BAL QB 22.62 0.42 9000
James Conner PIT RB 15.84 0.22 7100
David Johnson HOU RB 16.24 1.32 6400
David Montgomery CHI RB 15.49 0.91 5900
Davante Adams GBP WR 18.21 1.53 9000
Adam Thielen MIN WR 16.11 1.65 7400
Darius Slayton NYG WR 15.26 1.68 6000
T.J. Hockenson DET TE 9.20 0.00 5700
New York Jets NYJ DST 6.76 0.76 3500
85 138.33 8.78 Deshaun Watson HOU QB 20.66 0.22 8200
Derrick Henry TEN RB 18.44 0.66 9000
James Conner PIT RB 15.49 0.22 7100
David Montgomery CHI RB 14.35 0.91 5900
Adam Thielen MIN WR 18.45 1.65 7400
Jamison Crowder NYJ WR 15.37 1.12 6600
Tyler Boyd CIN WR 12.93 0.62 6100
Jimmy Graham CHI TE 11.24 1.90 5400
Cleveland Browns CLE DST 11.41 1.48 4300
796 139.11 9.01 Aaron Rodgers GBP QB 23.86 1.17 8400
Derrick Henry TEN RB 19.98 0.66 9000
James Conner PIT RB 15.70 0.22 7100
David Montgomery CHI RB 17.13 0.91 5900
Adam Thielen MIN WR 13.85 1.65 7400
Jamison Crowder NYJ WR 15.52 1.12 6600
Marquise Brown BAL WR 13.92 1.26 6300
Jimmy Graham CHI TE 12.00 1.90 5400
New York Giants NYG DST 7.14 0.12 3900
2788 142.44 9.47 Ben Roethlisberger PIT QB 22.48 1.00 7600
Derrick Henry TEN RB 19.03 0.66 9000
James Conner PIT RB 15.68 0.22 7100
David Montgomery CHI RB 15.34 0.91 5900
Adam Thielen MIN WR 18.43 1.65 7400
Terry McLaurin WAS WR 16.15 1.04 6900
Tyler Boyd CIN WR 12.63 0.62 6100
Jimmy Graham CHI TE 13.02 1.90 5400
Pittsburgh Steelers PIT DST 9.69 1.48 4500
3018 142.84 10.10 Aaron Rodgers GBP QB 23.81 1.17 8400
Derrick Henry TEN RB 19.94 0.66 9000
Alexander Mattison MIN RB 18.37 2.73 7000
David Montgomery CHI RB 15.16 0.91 5900
Calvin Ridley ATL WR 15.93 0.11 8600
Tyler Boyd CIN WR 12.91 0.62 6100
Randall Cobb HOU WR 11.70 0.52 5100
Jimmy Graham CHI TE 14.66 1.90 5400
Pittsburgh Steelers PIT DST 10.37 1.48 4500
4393 142.97 10.46 Lamar Jackson BAL QB 22.51 0.42 9000
Derrick Henry TEN RB 19.00 0.66 9000
David Montgomery CHI RB 14.77 0.91 5900
Devonta Freeman NYG RB 16.94 1.88 5600
Adam Thielen MIN WR 16.28 1.65 7400
Kenny Golladay DET WR 15.48 1.81 7200
Jamison Crowder NYJ WR 16.93 1.12 6600
Jimmy Graham CHI TE 13.68 1.90 5400
New York Giants NYG DST 7.36 0.12 3900
6100 144.44 10.93 Lamar Jackson BAL QB 23.23 0.42 9000
Derrick Henry TEN RB 20.11 0.66 9000
Kareem Hunt CLE RB 16.71 1.98 7000
David Montgomery CHI RB 15.55 0.91 5900
Adam Thielen MIN WR 19.61 1.65 7400
Tyler Boyd CIN WR 13.06 0.62 6100
Darius Slayton NYG WR 13.72 1.68 6000
Jimmy Graham CHI TE 13.41 1.90 5400
Tennessee Titans TEN DST 9.04 1.11 4000
293 145.08 10.99 Deshaun Watson HOU QB 21.11 0.22 8200
Derrick Henry TEN RB 19.27 0.66 9000
David Montgomery CHI RB 16.23 0.91 5900
Adrian Peterson DET RB 12.71 0.28 5500
Adam Thielen MIN WR 17.24 1.65 7400
Kenny Golladay DET WR 17.42 1.81 7200
Marquise Brown BAL WR 13.73 1.26 6300
Jimmy Graham CHI TE 13.78 1.90 5400
Indianapolis Colts IND DST 13.59 2.30 5000
8745 146.25 11.28 Lamar Jackson BAL QB 23.33 0.42 9000
Derrick Henry TEN RB 20.42 0.66 9000
David Montgomery CHI RB 15.02 0.91 5900
Devonta Freeman NYG RB 15.88 1.88 5600
Adam Thielen MIN WR 16.56 1.65 7400
Jamison Crowder NYJ WR 16.21 1.12 6600
Marquise Brown BAL WR 15.96 1.26 6300
Jimmy Graham CHI TE 12.24 1.90 5400
Cleveland Browns CLE DST 10.63 1.48 4300
3012 147.52 11.55 Lamar Jackson BAL QB 22.60 0.42 9000
Derrick Henry TEN RB 19.34 0.66 9000
Joe Mixon CIN RB 17.98 1.13 6900
David Montgomery CHI RB 15.50 0.91 5900
Kenny Golladay DET WR 15.82 1.81 7200
Robby Anderson CAR WR 18.19 1.93 6500
Darius Slayton NYG WR 14.36 1.68 6000
Jimmy Graham CHI TE 14.31 1.90 5400
Tennessee Titans TEN DST 9.41 1.11 4000
2910 149.05 11.79 Aaron Rodgers GBP QB 24.09 1.17 8400
Derrick Henry TEN RB 19.18 0.66 9000
Kareem Hunt CLE RB 18.84 1.98 7000
David Montgomery CHI RB 17.27 0.91 5900
Terry McLaurin WAS WR 14.81 1.04 6900
Jamison Crowder NYJ WR 14.65 1.12 6600
Brandin Cooks HOU WR 11.25 0.70 5700
Jimmy Graham CHI TE 14.89 1.90 5400
Indianapolis Colts IND DST 14.06 2.30 5000
6835 152.16 12.17 Deshaun Watson HOU QB 21.13 0.22 8200
Derrick Henry TEN RB 18.75 0.66 9000
James Conner PIT RB 15.68 0.22 7100
David Montgomery CHI RB 16.27 0.91 5900
Adam Thielen MIN WR 16.67 1.65 7400
Jamison Crowder NYJ WR 15.62 1.12 6600
Marquise Brown BAL WR 14.48 1.26 6300
Tyler Eifert JAC TE 19.30 3.83 4500
Indianapolis Colts IND DST 14.27 2.30 5000
5332 153.78 14.14 Lamar Jackson BAL QB 22.60 0.42 9000
Kareem Hunt CLE RB 19.26 1.98 7000
Alexander Mattison MIN RB 21.89 2.73 7000
David Montgomery CHI RB 15.50 0.91 5900
Adam Thielen MIN WR 17.19 1.65 7400
Terry McLaurin WAS WR 14.20 1.04 6900
Jamison Crowder NYJ WR 15.94 1.12 6600
Jimmy Graham CHI TE 13.38 1.90 5400
New England Patriots NEP DST 13.82 2.40 4800
880 154.17 15.90 Cam Newton NEP QB 33.15 6.28 8000
Derrick Henry TEN RB 19.25 0.66 9000
James Conner PIT RB 16.03 0.22 7100
David Montgomery CHI RB 16.15 0.91 5900
Adam Thielen MIN WR 15.70 1.65 7400
Marquise Brown BAL WR 13.33 1.26 6300
Tyler Boyd CIN WR 13.66 0.62 6100
Jimmy Graham CHI TE 12.80 1.90 5400
New England Patriots NEP DST 14.11 2.40 4800
3687 156.98 16.04 Lamar Jackson BAL QB 22.61 0.42 9000
Kareem Hunt CLE RB 21.04 1.98 7000
Alexander Mattison MIN RB 22.90 2.73 7000
David Montgomery CHI RB 15.28 0.91 5900
Julio Jones ATL WR 20.92 3.58 8200
Jamison Crowder NYJ WR 16.33 1.12 6600
Robby Anderson CAR WR 15.19 1.93 6500
Jimmy Graham CHI TE 14.28 1.90 5400
Cleveland Browns CLE DST 8.43 1.48 4300
9146 158.13 16.33 Nick Foles CHI QB 23.67 2.92 6900
Derrick Henry TEN RB 19.91 0.66 9000
Kareem Hunt CLE RB 18.78 1.98 7000
Devonta Freeman NYG RB 17.00 1.88 5600
Adam Thielen MIN WR 17.57 1.65 7400
Jamison Crowder NYJ WR 14.59 1.12 6600
Robby Anderson CAR WR 17.97 1.93 6500
Jimmy Graham CHI TE 13.45 1.90 5400
Indianapolis Colts IND DST 15.19 2.30 5000
1611 158.58 16.48 Joe Flacco NYJ QB 34.35 4.07 6500
Derrick Henry TEN RB 20.73 0.66 9000
Kareem Hunt CLE RB 17.99 1.98 7000
Devonta Freeman NYG RB 15.31 1.88 5600
Davante Adams GBP WR 18.68 1.53 9000
Adam Thielen MIN WR 15.73 1.65 7400
Randall Cobb HOU WR 10.14 0.52 5100
Jimmy Graham CHI TE 12.19 1.90 5400
Indianapolis Colts IND DST 13.46 2.30 5000
2548 159.29 16.80 Cam Newton NEP QB 42.75 6.28 8000
Derrick Henry TEN RB 19.39 0.66 9000
Kareem Hunt CLE RB 17.98 1.98 7000
David Montgomery CHI RB 17.44 0.91 5900
Davante Adams GBP WR 18.72 1.53 9000
Jamison Crowder NYJ WR 13.54 1.12 6600
Brandin Cooks HOU WR 11.06 0.70 5700
Jimmy Graham CHI TE 11.50 1.90 5400
Detroit Lions DET DST 6.92 1.70 3400
7704 161.04 17.52 Cam Newton NEP QB 36.41 6.28 8000
Derrick Henry TEN RB 19.40 0.66 9000
Kareem Hunt CLE RB 18.90 1.98 7000
Devonta Freeman NYG RB 13.93 1.88 5600
Adam Thielen MIN WR 18.87 1.65 7400
DeVante Parker MIA WR 12.62 0.25 6400
Tyler Boyd CIN WR 13.54 0.62 6100
Jimmy Graham CHI TE 13.49 1.90 5400
Indianapolis Colts IND DST 13.88 2.30 5000
4196 162.19 19.59 Nick Foles CHI QB 22.85 2.92 6900
Derrick Henry TEN RB 20.23 0.66 9000
Kareem Hunt CLE RB 18.25 1.98 7000
Frank Gore NYJ RB 30.27 5.02 5000
Calvin Ridley ATL WR 15.83 0.11 8600
Adam Thielen MIN WR 16.73 1.65 7400
Jamison Crowder NYJ WR 14.30 1.12 6600
Tyler Eifert JAC TE 13.63 3.83 4500
Indianapolis Colts IND DST 10.09 2.30 5000
3384 167.15 19.87 Cam Newton NEP QB 38.44 6.28 8000
Derrick Henry TEN RB 19.77 0.66 9000
James Conner PIT RB 15.76 0.22 7100
Devonta Freeman NYG RB 15.58 1.88 5600
Adam Thielen MIN WR 19.76 1.65 7400
Jamison Crowder NYJ WR 14.62 1.12 6600
Robby Anderson CAR WR 15.73 1.93 6500
Tyler Eifert JAC TE 11.35 3.83 4500
Indianapolis Colts IND DST 16.15 2.30 5000
4102 167.60 20.25 Tom Brady TBB QB 26.30 4.79 7500
Derrick Henry TEN RB 18.44 0.66 9000
Joe Mixon CIN RB 16.63 1.13 6900
Frank Gore NYJ RB 30.06 5.02 5000
Adam Thielen MIN WR 19.19 1.65 7400
Jamison Crowder NYJ WR 15.49 1.12 6600
Darius Slayton NYG WR 14.08 1.68 6000
Jimmy Graham CHI TE 15.14 1.90 5400
Indianapolis Colts IND DST 12.28 2.30 5000
3585 167.64 21.97 Cam Newton NEP QB 43.49 6.28 8000
Derrick Henry TEN RB 20.18 0.66 9000
Alexander Mattison MIN RB 17.97 2.73 7000
David Montgomery CHI RB 16.08 0.91 5900
Julio Jones ATL WR 18.52 3.58 8200
Jamison Crowder NYJ WR 14.38 1.12 6600
Julian Edelman NEP WR 16.22 3.08 6200
Jimmy Graham CHI TE 12.88 1.90 5400
Detroit Lions DET DST 7.92 1.70 3400
6980 167.76 24.93 Cam Newton NEP QB 34.20 6.28 8000
Alexander Mattison MIN RB 22.25 2.73 7000
Devonta Freeman NYG RB 15.16 1.88 5600
Frank Gore NYJ RB 21.08 5.02 5000
Davante Adams GBP WR 17.47 1.53 9000
Calvin Ridley ATL WR 15.84 0.11 8600
Marquise Brown BAL WR 17.14 1.26 6300
Tyler Eifert JAC TE 11.72 3.83 4500
Indianapolis Colts IND DST 12.89 2.30 5000